package com.sc.test.arouter;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.facade.callback.NavCallback;
import com.alibaba.android.arouter.launcher.ARouter;
import com.sc.test.R;

import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Shen Chao
 * Created by 2019-02-11
 */
public class ARouterActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_arouter);
        ButterKnife.bind(this);
        ARouter.getInstance().inject(this);
    }

    @OnClick({R.id.btn1, R.id.btn2})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn1:
                MessageBean.clear();
                ARouter.getInstance().build("/app/resultActivity").greenChannel().navigation(this, 111, new NavCallback() {
                    @Override
                    public void onArrival(Postcard postcard) {
                        MessageBean.setMessage("onArrival 路由已到达");
                        MessageBean.setMessage("greenChannel() 绿色通道，直接跳转，不走拦截器");
                        Log.d("-----", "onArrival 路由已到达");
                    }

                    @Override
                    public void onFound(Postcard postcard) {
                        MessageBean.setMessage("onFound 路由找到目标");
                        Log.d("-----", "onFound 路由找到目标");
                    }
                });
                break;
            case R.id.btn2:
                MessageBean.clear();
                ARouter.getInstance()
                        .build("/app/resultActivity")
                        .withBoolean("flag", true)
                        .withInt("age", 16)
                        .withString("name", "hello")
                        .withParcelable("fatUser", new UserBean("tom", 22))
                        .navigation(this, 222, new NavCallback() {
                            @Override
                            public void onArrival(Postcard postcard) {
                                MessageBean.setMessage("onArrival 路由已到达");
                                Log.d("-----", "onArrival 路由已到达");
                            }

                            @Override
                            public void onFound(Postcard postcard) {
                                MessageBean.setMessage("onFound 路由找到目标");
                                Log.d("-----", "onFound 路由找到目标");
                            }
                        });
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 111:
                Toast.makeText(this, "从无参绿色通道回来", Toast.LENGTH_SHORT).show();
                break;
            case 222:
                Toast.makeText(this, "从有参带拦截器回来", Toast.LENGTH_SHORT).show();
                break;
        }
    }
}
